<?php
include_once("../includes/common.php");
include_once("../class/User.class.php");
include_once("../class/Comments.class.php");
require_once("../islogin.php");

if ($isadmin!=1){
    header('Location: /');
    exit;
}

$menuavatar = !$user_info["message"]["avatar"] ? "/data/avatar/default.jpg" : $user_info["message"]["avatar"];


//页面配置
$nowPage = $_GET['page'] ?? 1;
if($nowPage<0){
    exit('page error');
}
$pageName = "用户评论";
$pageNum = 12;
$totalPage = 1;

//页面数据
$comments = new Comments();
$res = $comments->getAllComments($nowPage, $pageNum);

$totalPage = $res['totalPages'];

if (isset($_GET['delcid'])) {
    // 在这里执行删除操作
    $comments->deleteComments($_GET['nuid'],$_GET['delcid']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER']; // 获取来路页面的 URL
    header("Location: $referrer"); // 重定向到来路页面
    exit();
}

if($_GET['so']){
    $searchKeyword = $_GET['so'] ?? ''; // 获取搜索关键词
    $totalPage = 1;
    $sodataArray = $comments->getAllComments(1,5000);
    $filteredResults = array_filter($sodataArray['data'], function($item) use ($searchKeyword) {
        return stripos($item['nickname'], $searchKeyword) !== false || stripos($item['title'], $_GET['so']) !== false || stripos($item['content'], $_GET['so']) !== false; // 不区分大小写的模糊匹配

    });
    //var_dump($filteredResults);exit;
    $filteredResults = array_values($filteredResults);  //编制索引
    $res['data'] = $filteredResults;

}


require_once('page.php');


?>

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <?php require_once('head.php'); ?>
    <style>
        .text-del-line{
            text-decoration: line-through;
        }
    </style>
</head>
<body>

<!--==================== Preloader Start ====================-->
<div class="preloader">
    <div class="loader"></div>
</div>
<!--==================== Preloader End ====================-->

<!--==================== Sidebar Overlay End ====================-->
<div class="side-overlay"></div>
<!--==================== Sidebar Overlay End ====================-->

<?php require_once('menu.php'); ?>

<div class="dashboard-main-wrapper">
    <?php require_once('top_navbar.php'); ?>

    <div class="dashboard-body">

        <div class="row gy-4">
            <div class="card">
                <div class="card-body">
                    <h4 class="mb-12"><?php echo $pageName;?></h4>

                    <h6 class="mb-12">共<?php echo $res['totalCount'];?>条数据 <?php echo $nowPage?>/<?php echo $totalPage?> 页</h6>


                    <?php for ($i = 0; $i < count($res['data']); $i++) { ?>
                        <div class="rounded-8 border border-gray-100 p-16 mb-16">
                            <div class="comments-box__content flex-between gap-8">
                                <div class="flex-align align-items-start gap-12">
                                    <img src="<?php echo '/' . ltrim($res['data'][$i]["avatar"] ?: '/data/avatar/default.jpg', '/'); ?>" class="w-32 h-32 rounded-circle object-fit-cover flex-shrink-0" alt="User Image">
                                    <div>
                                        <h6 class="text-lg mb-8"><?php echo $res['data'][$i]['nickname'];?></h6>
                                        <?php

                                        $title = $res['data'][$i]['title'];
                                        $comments_content = $res['data'][$i]['content'];

                                        $iparea = getIpProvince($res['data'][$i]['publish_ip']);
                                        $ip = long2ip($res['data'][$i]['publish_ip']);

                                        $nid = $res['data'][$i]['note_id'];
                                        $nuid = $res['data'][$i]['note_user_id'];
                                        $cid = $res['data'][$i]['comments_id'];


                                        echo "<a href='/explore.php?id={$nid}'><p class='text-gray-600 text-15'>在 $iparea 对笔记 《 $title 》 评论了：【 $comments_content 】 IP：$ip</p></a>";

                                        echo " 时间：".formatTimestampToDate($res['data'][$i]['comments_time']);


                                        ?>
                                    </div>
                                </div>
                                <a href="?delcid=<?php echo $cid;?>&nuid=<?php echo $nuid;?>" type="button" class="flex-shrink-0 fw-bold text-13 text-danger-600 flex-center gap-8 hover-text-danger-800">删除 <i class="ph ph-x d-flex text-lg"></i> </a>
                            </div>
                        </div>
                    <?php } ?>

                    <?php if(!$_GET['so']){?>

                        <div class="flex-between flex-wrap gap-8 mt-20">
                            <a href="<?php echo $prevPageLink; ?>" class="btn btn-outline-gray rounded-pill py-9 flex-align gap-4">
                                <span class="d-flex text-xl"><i class="ph ph-arrow-left"></i></span>
                                上一页
                            </a>

                            <?php if($totalPage>10){?>
                                <ul class="pagination flex-align flex-wrap">
                                    <?php include 'pagination.php'; ?>
                                </ul>
                            <?php }?>

                            <a href="<?php echo $nextPageLink; ?>" class="btn btn-outline-main rounded-pill py-9 flex-align gap-4">
                                下一页 <span class="d-flex text-xl"><i class="ph ph-arrow-right"></i></span>
                            </a>
                        </div>
                    <?php }?>
                </div>
            </div>

        </div>

    </div>

    <?php require_once('footer.php'); ?>
</div>


<?php require_once('foot.php'); ?>

</body>
</html>